package com.augurit.wf.bi.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.base.extmodel.ExtResult;
import com.augurit.common.sysmeta.service.ISysMetaColumnService;
import com.augurit.common.sysmeta.service.ISysMetaTableService;
import com.augurit.common.sysmeta.service.impl.SysMetaColumnServiceImpl;
import com.augurit.common.sysmeta.web.SysMetaColumnAction;
import com.augurit.common.sysmeta.web.form.SysMetaColumnForm;
import com.augurit.common.util.HqlUtils;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.service.IWfViewConfigService;
import com.augurit.wf.bi.web.form.WfViewConfigForm;

@SuppressWarnings("serial")
@Results({
	@Result(name = WfViewConfigAction.RELOAD, location = "wf-view-config.action", type = "redirect"),
	@Result(name = WfViewConfigAction.SUCCESS, location = "/jj/WfViewConfig/WfViewConfig.jsp"),
	@Result(name = WfViewConfigAction.INPUT, location = "/jj/WfViewConfig/WfViewConfig-input.jsp")
})
public class WfViewConfigAction extends ExtCrudActionSupport<WfViewConfigForm>{
	public static final String RELOAD = "reload";
	
	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	
	@Autowired
	private IWfViewConfigService wfViewConfigService;
	
	@Autowired
	private ISysMetaColumnService sysMetaColumnService;
	
	private WfViewConfigForm form;
	private Long id;
	private Long[] checkedIds;
	private Page<WfViewConfigForm> page = new Page<WfViewConfigForm>(10);
	
	private Long templateId;
	
	public String getByTemplateId() throws Exception{
		if(templateId != null){
			WfViewConfigForm viewConfigForm = wfViewConfigService.getByTemplateId(templateId);
			if(viewConfigForm == null){
				viewConfigForm = newViewConfig(templateId);
			}
			WfViewConfigAction.extRenderFormJson(viewConfigForm);
		}
		return null;
	}
	
	private WfViewConfigForm newViewConfig(Long templateId){
		WfViewConfigForm viewConfigForm = new WfViewConfigForm();
		viewConfigForm.setMasterEntity(wfBusTemplateService.get(templateId).getMasterEntity());
		viewConfigForm.setTemplateId(templateId);
		return viewConfigForm;
	}
	
	
	public void getColumnsByTablesAndColumns() throws Exception{
		HqlUtils.TableColumnPair[] pairs = HqlUtils.constructTableColumnPairs(form.getSelectEntities(), form.getQueryProperties());
		List<SysMetaColumnForm> list = sysMetaColumnService.getColumnsByTablesAndColumns(pairs);
		
		if(list != null && list.size()>0){
			String jsonString = Struts2Utils.getJsonString(list);
			SysMetaColumnAction.renderText(jsonString);
		}
	}
	
	
	/**
	 * 分页查询
	 */
	public String list() throws Exception {
		wfViewConfigService.search(page, form);
		return SUCCESS;
	}

	/**
	 * 新增或修改
	 */
	@Override
	public String input() throws Exception {
		return INPUT;
	}

	/**
	 * 保存新增或者修改的Form对象
	 */
	public String save() throws Exception {
		wfViewConfigService.save(form);
		WfViewConfigAction.extRenderSuccess();
		return null;
	}

	/**
	 * 删除一条记录
	 */
	public String delete() throws Exception {
		wfViewConfigService.delete(id);
		return RELOAD;
	}
	
	/**
	 * 删除多条记录
	 */
	public String deleteMore() throws Exception{
		wfViewConfigService.delete(checkedIds);
		return RELOAD;
	}
	
	/**
	 * 准备页面模型
	 */
	protected void prepareModel() throws Exception {
		if(id != null){
			form = wfViewConfigService.get(id);
		}
		else
			form = new WfViewConfigForm();
	}
	

	public WfViewConfigForm getModel() {
		return form;
	}
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(Long[] checkedIds) {
		this.checkedIds = checkedIds;
	}
	
	public Page<WfViewConfigForm> getPage() {
		return page;
	}


	public void setPage(Page<WfViewConfigForm> page) {
		this.page = page;
	}
	public Long getTemplateId() {
		return templateId;
	}
	public void setTemplateId(Long templateId) {
		this.templateId = templateId;
	}
	public WfViewConfigForm getForm() {
		return form;
	}
	public void setForm(WfViewConfigForm form) {
		this.form = form;
	}
	
}
